﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPortWindow
{
    public enum Sensor
    {
        /// <summary>
        /// 右
        /// </summary>
        right,
        /// <summary>
        /// 左
        /// </summary>
        left,
        /// <summary>
        /// 右下
        /// </summary>
        rightBottom,
        /// <summary>
        /// 右上
        /// </summary>
        rightUp,
        /// <summary>
        /// 左下
        /// </summary>
        leftBottom,
        /// <summary>
        /// 左上
        /// </summary>
        leftUp,
    }

    public static class P7Helper
    {
        public static byte[] GetSensorChannel(Sensor sensor)
        {
            switch (sensor)
            {
                case Sensor.right:
                    return new byte[2] { 0x00, 0x01 };
                case Sensor.left:
                    return new byte[2] { 0x00, 0x04 };
                case Sensor.rightBottom:
                    return new byte[2] { 0x00, 0x10 };
                case Sensor.rightUp:
                    return new byte[2] { 0x00, 0x20 };
                case Sensor.leftBottom:
                    return new byte[2] { 0x00, 0x40 };
                case Sensor.leftUp:
                    return new byte[2] { 0x00, 0x80 };
            }
            return null;
        }

        // 写入缰绳右传感器的校准参数
        public static byte[] GetCalibrateBytes(string K, string B, Sensor sensor)
        {

            byte[] KBytes = new byte[6] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
            byte[] BBytes = new byte[6] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };

            byte[] bytes1 = Encoding.ASCII.GetBytes(K);
            byte[] bytes2 = Encoding.ASCII.GetBytes(B);

            if (bytes1.Length > 6)
            {
                MessageBox.Show($"参数错误，字符过多 {K}");
                return null;
            }

            if (bytes2.Length > 6)
            {
                MessageBox.Show($"参数错误，字符过多 {B}");
                return null;
            }

            Buffer.BlockCopy(bytes1, 0, KBytes, 6 - bytes1.Length, bytes1.Length);
            Buffer.BlockCopy(bytes2, 0, BBytes, 6 - bytes2.Length, bytes2.Length);

            string str1 = ToHexStrFromByte(KBytes);
            string str2 = ToHexStrFromByte(BBytes);

            Form1.Log($"发送了 K {K} B {B} [{str1} {str2}]");

            byte[] channel = GetSensorChannel(sensor);

            byte[] sendBytes = new byte[20];
            byte[] head = new byte[4] { 0x5A, 0x12, 0x05, 0x00 };
            byte[] end = new byte[2] { 0xAA, 0x55 };

            Buffer.BlockCopy(head, 0, sendBytes, 0, head.Length);
            Buffer.BlockCopy(channel, 0, sendBytes, 4, channel.Length);
            Buffer.BlockCopy(KBytes, 0, sendBytes, 6, KBytes.Length);
            Buffer.BlockCopy(BBytes, 0, sendBytes, 12, BBytes.Length);
            Buffer.BlockCopy(end, 0, sendBytes, 18, end.Length);

            return sendBytes;

            //Log.Info("发送停止：" + ToHexStrFromByte(writeData));
            //serialPort.Write(text);
        }


        /// <summary>
        /// 清除系数
        /// </summary>
        /// <param name="sensor"></param>
        public static byte[] GetClearBytes(Sensor sensor)
        {
            byte[] sendBytes = new byte[20];
            byte[] head = new byte[4] { 0x5A, 0x12, 0x05, 0x00 };
            byte[] channel = GetSensorChannel(sensor);
            byte[] data = new byte[12] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x31, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30 };
            byte[] end = new byte[2] { 0xAA, 0x55 };

            Buffer.BlockCopy(head, 0, sendBytes, 0, head.Length);
            Buffer.BlockCopy(channel, 0, sendBytes, 4, channel.Length);
            Buffer.BlockCopy(data, 0, sendBytes, 6, data.Length);
            Buffer.BlockCopy(end, 0, sendBytes, 18, end.Length);

            return sendBytes;
        }

        public static string ToHexStrFromByte(byte[] byteDatas)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < byteDatas.Length; i++)
            {
                builder.Append(string.Format("{0:X2}", byteDatas[i]));
            }
            return builder.ToString().Trim();

        }
    }
}
